import time

from selenium import webdriver
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

path = Service('./chromedriver')
driver = webdriver.Chrome(service=path)
driver.get('https://www.ly.com/')  # 打开同程网
driver.implicitly_wait(20)  # 隐式等待20s
driver.maximize_window()  # 最大化窗口
driver.find_element(By.XPATH, '//*[@title="国内机票"]').click()  # 点击"国内机票"
depart_city = driver.find_element(By.XPATH, '//*[@nulltips="请输入出发城市名称"]')  # 定位出发城市
# depart_city.clear()  # 清空出发城市输入框
actions = ActionChains(driver)
actions.click(depart_city).perform()  # 鼠标点击出发城市
depart_city.send_keys("深圳")  # 出发城市输入“深圳”
depart_city.send_keys(Keys.ENTER)  # 回车
arrive_city = driver.find_element(By.XPATH, '//*[@nulltips="请输入到达城市名称"]')  # 定位到达城市
actions.click(arrive_city).perform()  # 鼠标点击到达城市
arrive_city.send_keys('成都')  # 到达成熟输入“成都”
arrive_city.send_keys(Keys.ENTER)  # 回车
# 2025-01-09
# nulltips="请选择出发日期"
depart_time = driver.find_element(By.XPATH, '//*[@nulltips="请选择出发日期"]')  # 定位出发日期
actions.click(depart_time).perform()  # 点击出发日期
depart_time.send_keys('2025-01-09')  # 输入出发日期
driver.find_element(By.ID, 'airplaneSubmit').click()  # 点击「搜索」
time.sleep(3)
driver.save_screenshot('./机票列表.png')
# 定位机票价格
prices_ele = driver.find_elements(By.XPATH, '//*[@class="head-prices"]/strong/em')
prices = []
for i in prices_ele:
    prices.append(i.text)
prices_num = [price.replace('¥', '') for price in prices]
for j in prices_num:
    print(j)
time.sleep(5)
driver.quit()
